Appendix 2C: OpenBUGS code for the time-trend N-mixture model

This is similar to the unconstrained N-mixture model, but the pattern in abundance over space 
and time is constrained by having a fitted year effect (time trend) and a random transect effect.

Initial values: these need to be at or above the minimum number on each transect
list(N=c(0,29,38,17,20,10,7,0,12,14,3,3,0,1,1,30,35,54,12,16,13,4,2,26,11,3,0,1,3,0,29,28,30,19,8,28,1,1,7,24,9,2,0,1,2))

Model {

	# Detection model
	a.p ~ dnorm(0,1)		# intercept of logit of detection probability (p)
	s.j.p ~ dunif(0,1)		# standard deviation among surveys in logit p
	tau.j.p <- pow(s.j.p,-2)	# convert to precision

	for (j in 1:n.surveys.max) {
		re.j.p[j] ~ dnorm(0, tau.j.p)	# assign nightly random effect on detection prob.
		logit(p[j]) <- a.p+re.j.p[j]	# calc. prob of first capture
	}

	# Abundance model
	a.N ~ dnorm(0,1)		# prior for expected value of log(N) for average transect in 2012
	b.year ~ dnorm(0,1) 		# average annual change in log(N)
	s.trans.N ~ dunif(0,5)		# SD in log N among transects
	tau.trans.N <-pow(s.trans.N,-2)	# convert to precision
	
	for (i in 1:n.transects) {		# for each transect... 
		re.trans.N[i] ~ dnorm(0, tau.trans.N)	# assign random effect on log(N)
	}
	
	for (i in 1:n.rows) {		# for each transect for each year...
		log(mu[i]) <- a.N + b.year*(year[i]-2012) + re.trans.N[transect[i]]	# calculate expected N
		N[i] ~dpois(mu[i])																	# sample N from expected
		
		for (j in 1:n.surveys[i]) {	# for each sampling occasion
			n[i,j] ~ dbin(p[j],N[i])	# sample no. new found from no. unmarked present
		}
	}

	# Derived parameters 
	lambda <- exp(b.year)		# proportional annual change in abundance 
	lambda9 <- pow(lambda,9)	# change in abdundance over 9 year timeframe 

}